﻿using AutoMapper;
using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Implement;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.Useres;
using MediatR;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.Useres
{
    public class UpdatePassWordCommandHandler : IRequestHandler<UpdatePassWordCommand, ApiResult<int>>
    {
        IBaseRepository<Users> Users;
        IMapper mapper;
        DatabaseContext db;

        public UpdatePassWordCommandHandler(IBaseRepository<Users> Users, IMapper mapper, DatabaseContext db)
        {
            this.Users = Users;
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<ApiResult<int>> Handle(UpdatePassWordCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            try
            {

                var slist = Users.GetQuery().Where(x => x.UId == request.UId).First();
   
               slist.User_PassWord = request.User_PassWord;
                slist.CreateTime = DateTime.Now;
                var cout = Users.UpdateAsync(slist).Result;
                if (cout > 0)
                {
                    apiResult.Code = ApiEnum.success;
                    apiResult.Message = "更新成功";
                    apiResult.Data = 1;
                }
                else
                {
                    apiResult.Code = ApiEnum.error;
                    apiResult.Message = "更新失败";
                    apiResult.Data = 0;
                }
            }

            catch (Exception ex)
            {
                apiResult.Code = ApiEnum.error;
                apiResult.Message = ex.Message;

            }

            return apiResult;
        }
    }
}
